﻿using System;
using System.Diagnostics;
using System.ServiceModel;
using StockholmsStad.Utilities.Exceptions;
using System.Web;

namespace StockholmsStad.Web.UI.Class.Utilities
{
    /// <summary>
    /// Utilityfunktioner för att hantera (listor av) datum och tider. 
    /// </summary>
    public class ErrorHandling
    {
        public static void HandleException(Exception e)
        {
            if (e is FaultException<StockholmStadSystemErrorFault>)
            {
                var errorInfo = (e as FaultException<StockholmStadSystemErrorFault>).Detail;

                if(errorInfo != null)
                {
                    string error = string.Format("Felkod: {0}, Felmeddelande: {1}", errorInfo.ErrorCode,
                                                 errorInfo.ErrorMessage);
                    ExceptionManager.WriteToEventLog(error, EventLogEntryType.Error);                    
                }
                else
                {
                    ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
                }
            }
            else if (e is FaultException<StockholmStadApplicationErrorFault>)
            {
                var errorInfo = (e as FaultException<StockholmStadApplicationErrorFault>).Detail;
                if (errorInfo != null)
                {
                    string error = string.Format("Felkod: {0}, Felmeddelande: {1}", errorInfo.ErrorCode,
                                                 errorInfo.ErrorMessage);
                    ExceptionManager.WriteToEventLog(error, EventLogEntryType.Error);
                }
                else
                {
                    ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
                }
            }
            else if (e is StockholmStadSystemException || e is FaultException<StockholmStadSystemException>)
            {
                var errorInfo = e as StockholmStadSystemException;
                if (errorInfo != null)
                {
                    string error = string.Format("Felkod: {0}, Felmeddelande: {1}", errorInfo.ErrorCode,
                                                 errorInfo.Message);
                    ExceptionManager.WriteToEventLog(error, EventLogEntryType.Error);
                }
                else
                {
                    ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
                }
            }
            else if (e is FaultException || e is FaultException<ExceptionDetail>)
            {
                ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
            }
            else if (e is CommunicationException)
            {
                ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
            }
            else if (e is StockholmStadApplicationException)
            {
                ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
            }
            else
            {
                // Should never occur....
                ExceptionManager.WriteToEventLog(e, EventLogEntryType.Error);
            }
        }
    }
}
